<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xiaobai
 * Date: 13-5-24
 * Time: 上午10:18
 * To change this template use File | Settings | File Templates.
 */
class Mobility_ip_error_model extends CI_Model {

    private $_write_db;
    function __construct()
    {
        parent::__construct();
        $this->load->library('error_code_library');
        $this->_write_db = $this->load->database('master', TRUE);
    }

    public function create_mobility_ip_error($id,$source) {
        $data = array(
            'mobility_ip_id'        => $id,
            'source'        => $source,
            'ctime'    => time()
        );
        $this->_write_db->insert('mobility_ip_error', $data);
    }

    public function update_mobility_ip_error_count($id = NULL, $error_string) {
        if(is_null($id)) {
            return $this->error_code_library->getError('system', 'params', 'no_must_parameter', 'ID In Function '. __FUNCTION__);
        } else {
            $erro_type = $this->get_error_type($error_string);
            $this->_write_db->set($erro_type, $erro_type.'+1', FALSE);
            $this->_write_db->set('error_sum', 'error_sum+1', FALSE);
            $this->_write_db->where('`mobility_ip_id` = '.$id);
            $result = $this->_write_db->update('mobility_ip_error');
        }
    }

    public function get_error_type($error_string = NULL) {
        if(strpos($error_string, 'connect to host') || strpos($error_string, 'connect() timed out')) {
            $error_type = 'connect_host_error';
        } else if(strpos($error_string, 'SOCKS5 connect request ack')) {
            $error_type = 'socks5_request_ack';
        } else if(strpos($error_string, 'initial SOCKS5 response')) {
            $error_type = 'socks5_response';
        } else if(strpos($error_string, 'timed out after')) {
            $error_type = 'timeout';
        } else if(strpos($error_string, 'verified_code')) {
            $error_type = 'verified_code';
        }else {
            $error_type = 'others';
        }

        return $error_type;
    }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
    * 查看ip 在指定时间段内 网络请求错误数
    *
    * @access public
    * @param array $time_rang_type 要查看的时间范围类别 today 当天  week 本周 month 本月 [Optional]
    * @return array $mixed
    */
    public function get_error_count($time_rang_type = 'today') {
		$sql = "SELECT a.source, SUM( sina_use_count ) AS use_sum, SUM(  `error_sum` ) AS error_sum,";
		$sql .= "SUM(timeout) AS timeout,SUM(socks5_request_ack) AS socks5_request_ack,SUM(socks5_response) AS socks5_response,";
		$sql .= "SUM(connect_host_error) AS connect_host_error,SUM(verified_code) AS verified_code,SUM(others) AS others";
		$sql .= " FROM  `mobility_ip_error` AS a, mobility_ip AS b";
		$sql .= " WHERE a.`mobility_ip_id` = b.`id`";

		switch ($time_rang_type) {
			case 'today':
				$sql .= " AND a.ctime > UNIX_TIMESTAMP( CURDATE( ) )";
				break;
			case 'week':
				$sql .= " AND a.ctime > unix_timestamp(DATE_ADD(CURDATE(), INTERVAL -7 DAY))";
				break;
			case 'month':
				$sql .= " AND a.ctime > unix_timestamp(DATE_ADD(CURDATE(), INTERVAL -30 DAY))";
				break;
			default:
				$sql .= " AND a.ctime > UNIX_TIMESTAMP( CURDATE( ) )";
				break;
		}

		$sql .= " GROUP BY a.`source`";
		$sql .= " ORDER BY use_sum DESC , error_sum DESC";

		return $this->db->query($sql)->result_array();
    }
}